#include "tcp_server.h"
#include "ui_tcp_server.h"
#include <QString>
#include <QDebug>
tcp_server::tcp_server(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::tcp_server)
{
    ui->setupUi(this);

    tcpSever = new QTcpServer(this);
    tcpSocket = new QTcpSocket(this);

    connect(tcpSever, SIGNAL(newConnection()), this, SLOT(newConnection_SLOT()));
}
void tcp_server::newConnection_SLOT(void)
{
    tcpSocket = tcpSever->nextPendingConnection();

}
void tcp_server::socketReadReadySLOT()
{
    QString readdata;

    readdata = tcpSocket->readAll();
    ui->plainTextEdit->appendPlainText(readdata);
}
tcp_server::~tcp_server()
{
    delete ui;
}


void tcp_server::on_pushButton_open_sever_clicked()
{
    tcpSever->listen(QHostAddress::Any, ui->lineEdit_port_num->text().toInt());
    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(socketReadReadySLOT()));
}

void tcp_server::on_pushButton_close_sever_clicked()
{
    tcpSever->close();
}

void tcp_server::on_pushButton_end_data_clicked()
{
    tcpSocket->write(ui->lineEdit_send_data->text().toLocal8Bit());
}
